package ar.com.solidussource.howmuchmobile.services.helper;

import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.message.BasicNameValuePair;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: FT31270775
 * Date: 06/11/12
 * Time: 16:03
 * To change this template use File | Settings | File Templates.
 */
public class RestServiceHelper {

    public static String addParamToUrl(String url, HashMap<String, Object> paramsMap){
        if(!url.endsWith("?"))
            url += "?";

        List<NameValuePair> params = new LinkedList<NameValuePair>();

        Iterator<String> ite = paramsMap.keySet().iterator();

        while (ite.hasNext()) {
            String key = ite.next();
            Object param = paramsMap.get(key);

            params.add(new BasicNameValuePair(key, String.valueOf(param)));
        }

        String paramString = URLEncodedUtils.format(params, "utf-8");

        url += paramString;

        return url;
    }
}
